# 单调栈
def maxProfit(prices):
    stack = []
    max_profit = 0
    for price in prices:
        if not stack:
            stack.append(price)
        while stack and stack[-1] > price:
            stack.pop()
        stack.append(price)
        max_profit = max(max_profit, price - stack[0])
    return max_profit


print(maxProfit([5, 1, 2, 3, 4, 10]))
print(maxProfit([6, 5, 3, 1]))
